package com.chen.staffjoy.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.chen.staffjoy.account.dao.AccountDao;
import com.chen.staffjoy.account.dto.AccountDto;
import com.chen.staffjoy.account.dto.AccountList;
import com.chen.staffjoy.account.entity.Account;
import com.chen.staffjoy.account.service.IAccountService;
import com.chen.staffjoy.commonlib.base.BaseServiceImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 账户信息表 接口实现
 *
 * @author ChenTian
 */ 
@Service
public class AccountServiceImpl extends BaseServiceImpl<Account> implements IAccountService {

	@Resource
	private AccountDao accountDao;

	@Override
	public AccountDto create(String name, String email, String phoneNumber) {
		Account account = Account.builder()
				.id(IdUtil.getSnowflakeNextIdStr())
				.name(name)
				.email(email)
				.phoneNumber(phoneNumber)
				.memberSince(Instant.now())
				.build();

		accountDao.save(account);
		AccountDto accountDto = BeanUtil.toBean(account, AccountDto.class);
		return accountDto;
	}

	@Override
	public AccountDto getOrCreate(String name, String email, String phoneNumber) {

		Account account = accountDao.findByPhoneNumber(phoneNumber);
		if(account != null){
			return BeanUtil.toBean(account, AccountDto.class);
		}

		return create(name, email, phoneNumber);
	}

	@Override
	public AccountList list(int offset, int limit) {
		PageRequest pageRequest = PageRequest.of(offset, limit, Sort.by(Sort.Order.desc("id")));
		Page<Account> accountPage = accountDao.findAll(pageRequest);

		List<Account> content = accountPage.getContent();
		List<AccountDto> accountDtoList = null;
		if(CollectionUtil.isNotEmpty(content)){
			accountDtoList = content.stream().map(c -> BeanUtil.toBean(c, AccountDto.class))
					.collect(Collectors.toList());
		}

		return AccountList.builder()
				.limit(limit)
				.offset(offset)
				.accounts(accountDtoList)
				.build();
	}

	@Override
	public AccountDto get(String userId) {
		Account account = findById(userId);
		AccountDto accountDto = BeanUtil.toBean(account, AccountDto.class);
		return accountDto;
	}

	@Override
	public AccountDto getAccountByPhoneNumber(String phoneNumber) {
		Account account = accountDao.findByPhoneNumber(phoneNumber);
		AccountDto accountDto = BeanUtil.toBean(account, AccountDto.class);
		return accountDto;
	}

	@Override
	public AccountDto update(AccountDto newAccount) {
		Account account = findById(newAccount.getId());
		account.setName(newAccount.getName());
		account.setEmail(newAccount.getEmail());
		account.setPhoneNumber(newAccount.getPhoneNumber());
		accountDao.save(account);

		AccountDto accountDto = BeanUtil.toBean(account, AccountDto.class);
		return accountDto;
	}

	@Override
	public void updatePassword(String userId, String password) {
		Account account = findById(userId);
		String pwdHash = SecureUtil.md5(password);
		account.setPasswordHash(pwdHash);
		accountDao.save(account);
	}


}
